"""
Case Type   : 基础功能-兼容语法
Case Name   : show function status语句查询函数信息
Create At   : 2024/10/22
Owner       : peilinqian
Description :
    step1: 查询，显示opengauss侧内置函数;
    step2: 创建自定义函数;
    step3: 查询，使用like匹配、使用where条件;
    step5: 修改函数名称;
    step6: 查询，Modified列时间变更;
    step7: Security_type字段验证，f为INVOKER，t为DEFINER;
Expect      :
    step1: 查询，显示opengauss侧内置函数; expect:成功
    step2: 创建自定义函数; expect:成功
    step3: 查询，使用like匹配、使用where条件; expect:成功
    step5: 修改函数名称; expect:成功
    step6: 查询，Modified列时间变更; expect:成功
    step7: Security_type字段验证，f为INVOKER，t为DEFINER; expect:结果为false
History     :
    modified by @xuwenfei1 2025-3.19:修改show function status like 'array%';操作的期望行数：expect：71行
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Grammar(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:初始化----')
        self.pri_sh = CommonSH('PrimaryDbBmode')
        self.constant = Constant()
        self.func_name = 'f_compatibility_grammar_show_case0001'

    def test_main(self):
        step_txt = '----step1: 查询，显示opengauss侧内置函数; expect:成功----'
        self.log.info(step_txt)
        sql = f"show function status like 'array%'; "
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn('82 rows', result, "执行失败" + step_txt)

        step_txt = '----step2: 创建自定义函数; expect:成功----'
        self.log.info(step_txt)
        sql = f"drop function if exists {self.func_name}; \n" \
              f"create function {self.func_name}(integer, integer) " \
              f"returns integer comment 'co自定义函数0001' \n" \
              f"as 'select \$1 + \$2;' \n" \
              f"language sql \n" \
              f"immutable \n" \
              f"returns null on null input;"
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn(self.constant.CREATE_FUNCTION_SUCCESS_MSG, result,
                      "执行失败" + step_txt)

        step_txt = '----step3: 查询，使用like匹配、使用where条件; expect:成功----'
        self.log.info(step_txt)
        sql = f"show function status where \"Name\" like'%{self.func_name}';"
        result_1 = self.pri_sh.execut_db_sql(sql)
        self.log.info(result_1)
        sql = f"show function status where \"Name\" = '{self.func_name}' " \
              f"and \"Db\"='public';"
        result_2 = self.pri_sh.execut_db_sql(sql)
        self.log.info(result_2)
        self.assertIn('1 row', result_1, "执行失败" + step_txt)
        self.assertEqual(result_1, result_2, "执行失败" + step_txt)

        step_txt = '----step5: 修改函数名称; expect:成功----'
        self.log.info(step_txt)
        alter_sql = f"alter function {self.func_name}(integer, integer) " \
                    f"rename to {self.func_name}_1;"
        result = self.pri_sh.execut_db_sql(alter_sql)
        self.log.info(result)
        self.assertIn(self.constant.ALTER_FUNCTION_SUCCESS_MSG, result,
                      "执行失败" + step_txt)

        step_txt = '----step6: 查询，Modified列时间变更; expect:成功----'
        self.log.info(step_txt)
        sql = f"show function status where \"Name\" like'%{self.func_name}_1';"
        result_check = self.pri_sh.execut_db_sql(sql)
        self.log.info(result_check)
        sql = f"select ctime from pg_object where object_oid=(" \
              f"select oid from pg_proc where proname='{self.func_name}_1');"
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        create_time = result.splitlines()[-2].strip()
        sql = f"select mtime from pg_object where object_oid=(" \
              f"select oid from pg_proc where proname='{self.func_name}_1');"
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        modified_time = result.splitlines()[-2].strip()
        self.assertIn(create_time, result_check, '执行失败:' + step_txt)
        self.assertIn(modified_time, result_check, '执行失败:' + step_txt)

        step_txt = '----step7: Security_type字段验证，f为INVOKER，t为DEFINER; ' \
                   'expect:结果为false----'
        self.log.info(step_txt)
        sql = f"select prosecdef from pg_proc where " \
              f"proname ='{self.func_name}_1';"
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertEqual('f', result.splitlines()[-2].strip(),
                         '执行失败:' + step_txt)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        step_txt = '----step:清理数据; expect:清理成功----'
        self.log.info(step_txt)
        drop_sql = f"drop function if exists {self.func_name};" \
                   f"drop function if exists {self.func_name}_1;"
        result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(result)

        self.log.info('----teardown断言----')
        self.assertIn(self.constant.DROP_FUNCTION_SUCCESS_MSG, result,
                      "执行失败" + step_txt)

        self.log.info(f'----{os.path.basename(__file__)}:执行完毕----')
